<template>
    <div class="tmpl goodsInfo">
        <transition name="ball"
        @before-enter="beforeEnter"
        @enter="enter"
        @after-enter="afterEnter">
            <div v-show="isShow" class="ball"></div>
        </transition>
        
        <div class="mui-card swipeContent">
            <!--内容区-->
            <div class="mui-card-content">
                <slider :imgs="imgList" :isfullWidth="false"></slider>
            </div>
        </div>
        <div class="mui-card infoContainer">
            <!--页眉，放置标题-->
            <div class="mui-card-header">{{goodsInfo.title}}</div>
            <!--内容区-->
            <div class="mui-card-content">
                <p>市场价:
                    <s>{{goodsInfo.market_price}}</s>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;销售价：
                    <span class="red">￥{{goodsInfo.sell_price}}</span>
                </p>
                <p>购买数量：&nbsp;&nbsp;<numberBox_info @getCount="getSelectCount" :max="goodsInfo.stock_quantity"></numberBox_info> </p>
                <p>
                    <mt-button type="primary" size="small">立即购买</mt-button>
                    <mt-button type="danger" size="small" @click="toShopCar">加入购物车</mt-button>
                </p>
            </div>
        </div>
        <div class="mui-card infoParam">
            <!--页眉，放置标题-->
            <div class="mui-card-header">商品参数</div>
            <!--内容区-->
            <div class="mui-card-content">
                <p>商品货号：&nbsp;{{goodsInfo.goods_no}}</p>
                <p>库存情况：&nbsp;{{goodsInfo.stock_quantity}}</p>
                <p>上架时间：&nbsp;{{goodsInfo.add_time}}</p>
            </div>
            <!--页脚，放置补充信息或支持的操作-->
            <div class="mui-card-footer">
                <mt-button type="primary" plain @click="goDesc()">图文介绍</mt-button>
                <mt-button type="danger" plain @click="goComment()">商品评论</mt-button>
            </div>
        </div>
    </div>
</template>
<script>
import slider from "../subcom/slider.vue";
import numberBox_info from "../subcom/numberBox_info.vue";
import common from "../../../statics/js/common.js";
export default {
  components: {
    slider,
    numberBox_info
  },
  data() {
    return {
      id: this.$route.params.id,
      isShow:false,
      selectCount:1,
      imgList: [],
      goodsInfo:{}
    };
  },
  created() {
    this.getImgs();
    this.getInfo();
  },
  methods: {
    getImgs() {
      this.$http
        .get(common.domain + "/api/getthumimages/" + this.id)
        .then(re => {
          if (re.body.status != "0") {
            this.$toast(re.body.message);
            return;
          }
          this.imgList = re.body.message;
        });
    },
    getInfo(){
        this.$http
        .get(common.domain + "/api/goods/getinfo/" + this.id)
        .then(re => {
          if (re.body.status != "0") {
            this.$toast(re.body.message);
            return;
          }
          this.goodsInfo = re.body.message[0];
        });
    },
    goDesc(){
        this.$router.push("/goods/goodsDesc/"+this.id);
    },
    goComment(){
        this.$router.push("/goods/goodsComment/"+this.id);
    },
    //实现小球进入购物车动画效果
    beforeEnter(el){
        el.style.transform = "translate(0px,0px)";
    },
    enter(el,done){
        el.offsetWidth;
        var currentX = parseInt(el.getBoundingClientRect().left);
        var currentY = parseInt(el.getBoundingClientRect().top);
        var aimX = parseInt(document.querySelector("#shopcarBox").getBoundingClientRect().left);
        var aimY = parseInt(document.querySelector("#shopcarBox").getBoundingClientRect().top);
        el.style.transform = `translate(${aimX-currentX}px,${aimY-currentY}px)`;
        el.style.transition = "all .3s cubic-bezier(.4,-0.3,1,.68)";
        done()
    },
    afterEnter(el){
        this.isShow = !this.isShow;
    },
    //加入购物车
    toShopCar(){
        this.isShow = !this.isShow;
        this.$store.commit('add',{
            id:this.id,
            count:this.selectCount,
            price:this.goodsInfo.sell_price,
            title:this.goodsInfo.title,
            stock_quantity:this.goodsInfo.stock_quantity,
            selected:true
        })
    },
    getSelectCount(count){
        this.selectCount = count;
    }
  }
};
</script>

<style lang="scss" scoped>
.goodsInfo {
  background-color: #eee;
  overflow: hidden;
  >div{
      overflow: hidden;
      p{
          margin:0;
          line-height: 30px;
      }
      .mui-card-content{
          padding:10px 15px;
      }
  }
  .swipeContent {
    .mui-card-content {
      height: 300px;
    }
  }
  .infoContainer {
    .mui-card-header {
      font-size: 16px;
      font-weight:bold;
      line-height: 20px;
    }
    .mui-card-content {
        .red{
            font-size:14px;
            font-weight:bold;
            color:red;
        }
    }
  }
  .infoParam {
    .mui-card-footer{
        display:block;
        button{
            width:100%;
            margin-top: 10px;
            display:block;
        }
    }
  }
  .ball{
      width:15px;
      height:15px;
      background-color:red;
      border-radius: 50%;
      position:absolute;
      z-index: 1000;
      top:476px;
      left:158px;
  }
}
</style>


